﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLite加解密2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtPassword.Text = "cd89c0d3417502e019e15d5a097a1f81";

            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "SQLite数据库文件|*.db";
        }

        //加密
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    EncryptDatabase(openFileDialog1.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"异常：{ex.Message}");
            }
        }

        //解密
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    DecryptDatabase(openFileDialog1.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        private void EncryptDatabase(string fileName)
        {
            using (var conn = new SQLiteConnection($"Data Source={fileName};Version=3;"))
            {
                conn.Open();
                conn.ChangePassword(txtPassword.Text);
            }
            MessageBox.Show($"加密成功");
        }

        /// <summary>
        /// 解密
        /// </summary>
        private void DecryptDatabase(string fileName)
        {
            using (var conn = new SQLiteConnection($"Data Source={fileName};Version=3;Password={txtPassword.Text}"))
            {
                conn.Open();
                conn.ChangePassword(string.Empty);
            }
            MessageBox.Show($"解密成功");
        }

        //拖放加密
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (radioButtonEncrypt.Checked)
                {
                    var data = e.Data?.GetData(DataFormats.FileDrop);
                    var fileName = ((string[])data)[0];
                    EncryptDatabase(fileName);
                }
                else
                {
                    var data = e.Data?.GetData(DataFormats.FileDrop);
                    var fileName = ((string[])data)[0];
                    DecryptDatabase(fileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"异常：{ex.Message}");
            }
        }

        //拖放
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
    }
}
